/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/11/23
 * Time: 22:33
 */
$(function(){
    var url = '/v1/manager/work/mark-list.json';
    var authorization = Cookies.get('_token');
    var page = 1;
    var page_size = 10;
    var score = '';
    var data = {
        page: page,
        page_size: page_size,
        order: 'desc',
        score: score
    };

    getDatas(url, data, authorization);
    $(document).on('click', '.paging_simple_numbers a', function(){
        data.page = $(this).attr('data-page');
        getDatas(url, data, authorization);
    });

    $('.score-sort').on('click', function(){
        if ($(this).find('i').hasClass('fa-sort')) {
            $(this).find('i').addClass('fa-sort-up').removeClass('fa-sort');
            data.sort = 'asc';
            getDatas(url, data, authorization);
        } else if ($(this).find('i').hasClass('fa-sort-up')) {
            $(this).find('i').addClass('fa-sort-down').removeClass('fa-sort-up');
            data.sort = 'desc';
            getDatas(url, data, authorization);
        } else if ($(this).find('i').hasClass('fa-sort-down')) {
            $(this).find('i').addClass('fa-sort').removeClass('fa-sort-down');
            data.sort = null;
            getDatas(url, data, authorization);
        }
    });
});

function getDatas(url, data, authorization) {
    $.ajax({
        type: 'get',
        url: url,
        data: data,
        headers: {
            Authorization: authorization,
            Accept: "application/json; charset=utf-8"
        },
        success: function(e){
            var data = JSON.parse(e);
            if (data.status == 200) {
                var html = '<tr class="no-data">';
                html += '<td colspan="5" class="text-center">暂无数据</td>';
                html += '</tr>';
                var json = data.body.data;
                if (json.rows.length > 0) {
                    html = '<tr class="no-data hidden">';
                    html += '<td colspan="5" class="text-center">暂无数据</td>';
                    html += '</tr>';
                    $.each(json.rows, function(n, v){
                        var score = v.score > 0 ? v.score : '未评分';
                        html += '<tr>';
                        html += '<td>'+v.id+'</td>';
                        html += '<td>'+v.realname+'</td>';
                        html += '<td>'+v.title+'</td>';
                        html += '<td>'+v.category_name+'</td>';
                        html += '<td>';
                        if (v.score_list.length > 0) {
                            $.each(v.score_list, function(n, v){
                                html += '<p><span>教师:'+v.teacher.realname+'</span><span>评分:'+v.score+'</span></p>';
                            });
                        }
                        html += '</td>';
                        html += '<td>'+v.score+'</td>';
                        html += '</tr>';
                    });
                }
                $('tr.no-data').parent().html('').append(html);

                var page = '<div class="row">';
                page += '<div class="col-sm-6">';
                if (json.rows.length > 0) {
                    page += '<div class="dataTables_info" role="alert" aria-live="polite" aria-relevant="all">显示 <span class="data-from">'+(json.offset+1)+'</span> 到 <span class="data-to">'+((json.page_number-1)*json.page_size+json.rows.length)+'</span> 项，共 <span class="data-total">'+json.total+'</span> 项</div>';
                }
                page += '</div>';
                if (json.pages > 1) {
                    page += '<div class="col-sm-6">';
                    page += '<div class="dataTables_paginate paging_simple_numbers">';
                    page += '<ul class="pagination" role="navigation">';

                    if (json.page_number == 1) {
                        page += '<li class="page-item disabled" aria-disabled="true" aria-label="pagination.previous"><span class="page-link" aria-hidden="true">‹</span></li>';
                    } else {
                        page += '<li class="page-item"><a class="page-link" href="javascript:;" data-page="'+(json.page_number-1)+'" rel="previous" aria-label="pagination.previous">‹</a></li>';
                    }
                    for (var k = 1; k <= json.pages; k ++) {
                        if (k == json.page_number) {
                            page += '<li class="page-item active" aria-current="page"><span class="page-link">'+k+'</span></li>';
                        } else {
                            page += '<li class="page-item"><a class="page-link" href="javascript:;" data-page="'+k+'">'+k+'</a></li>';
                        }
                    }

                    if (json.page_number < json.pages) {
                        page += '<li class="page-item"><a class="page-link" href="javascript:;" data-page="'+(json.page_number+1)+'" rel="next" aria-label="pagination.next">›</a></li>';
                    } else {
                        page += '<li class="page-item disabled" aria-disabled="true" aria-label="pagination.next"><span class="page-link" aria-hidden="true">›</span></li>';
                    }

                    page += '</ul>';
                    page += '</div>';
                    page += '</div>';
                }
                page += '</div>';
                $('.work-list .row').remove();
                $('.work-list').append(page);
            }
        }
    });
}